# coding: utf-8

import pandas as pd
df = pd.read_csv(r'F:\Work\Python\Pandas\study-pandas\file\datas\weather_20230115134249.csv')
print(df.head())

# 1. 直接给列赋值
# df.loc[:, '气温(度)']=df['气温(度)'].str.replace('℃','').astype('float64')
# print(df.dtypes)
# print(df.head())
# df.loc[:, 'diff']=df['气温(度)'] + df['相对湿度(%)'].astype('float64')
# print(df.head())

# 2. apply
# axis=0 或 'index'：按列操作（函数作用于每一列）
# axis=1 或 'columns'：按行操作（函数作用于每一行）
# def get_temp_type(x):
#     if x['气温(度)'] >'28℃':
#         return 'high temperature'
#     elif x['气温(度)'] <'14℃':
#         return 'low temperature'
#     else:
#         return 'normal temperature'
# df.loc[:, 'temp_type'] = df.apply(get_temp_type, axis=1)
# print(df.head())
# print(df['temp_type'].value_counts())

# 3. assign 不会修改原 DataFrame，而是返回一个新的 DataFrame
# df2 = df.assign(temperature = lambda x:x['相对湿度(%)']*10)
# print(df2.head())

# 4. 按条件赋值
df['temp_diff_type'] = ''
df.loc[df['相对湿度(%)'] > 70, 'temp_diff_type'] = '湿度高'
df.loc[df['相对湿度(%)'] < 30, 'temp_diff_type'] = '湿度低'
df.loc[(df['相对湿度(%)'] >=30) & (df['相对湿度(%)'] <=70), 'temp_diff_type'] = '湿度正常'
print(df.head())
print(df['temp_diff_type'].value_counts())
